from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('file:///D:/nsd2404/html/register.html')

sleep(3)
# 定位alert框
driver.find_element(By.CSS_SELECTOR, '#alert').click()
sleep(3)
# 处理对话框
# 线切换到对话框，在选择取消或者同意
alert = driver.switch_to.alert
# alert.accept()  # 同意
alert.dismiss() # 取消
# 输入用户名
driver.find_element(By.CSS_SELECTOR, '#user').send_keys('admin')

sleep(3)
driver.quit()